package My.project.Ui.Activity;

import My.project.Ui.R;
import My.project.Util.Connector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This activity is used to display the register view.
 * It can automatically check all fields of form,
 * and once registered, it will switch to LogInActivity.
 * @author Bell
 *
 */
public class RegisterActivity extends Activity {

	private Button registerButton;
	private Button resetButton;
	private EditText userName;
	private EditText nickname;
	private EditText password;
	private EditText confirm;
	private String name;
	private String nick;
	private String secret;
	private Bundle bundle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		// enable the home button
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		// Get the redirect message from LogInActivity
	    Intent intent = this.getIntent();
	    bundle = intent.getExtras();
		
		// get all edit text field
		userName = (EditText) findViewById(R.id.editText1);
		nickname = (EditText) findViewById(R.id.editText2);
		password = (EditText) findViewById(R.id.editText5);
		confirm = (EditText) findViewById(R.id.editText3);
		
		registerButton = (Button) findViewById(R.id.button_register);
		registerButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if(checkFields()) {
					// connect to server
					Connector con = new Connector();
					String res = con.register(name, nick, secret);
					
					showToast(res);
					if(res.contains("succeed")) { // if register successfully
						toLogIn();
					}
				}
			}
		});
		
		resetButton = (Button) findViewById(R.id.button_reset);
		resetButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				//clear all fields
				userName.setText("");
				nickname.setText("");
				password.setText("");
				confirm.setText("");
			}
		});
	}
	
	//menu button action
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.str_about);
		menu.add(0, 1, 1, R.string.str_exit);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case android.R.id.home:
            // This is called when the Home (Up) button is pressed
            // in the Action Bar.
            Intent parentActivityIntent = new Intent(
            		this, LogInActivity.class);
            parentActivityIntent.putExtras(bundle);
            parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(parentActivityIntent);
            finish();
            break;
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionsDialog();
			break;
		default:
			break;
		}
		return true;
	}
	
	// show author info
	private void openOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_about)
		.setMessage(R.string.app_about_msg)
		.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		}).show();
	}

	// show exit dialog
	private void exitOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_exit)
		.setMessage(R.string.app_exit_msg)
		.setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				Intent intent = new Intent(Intent.ACTION_MAIN);
  	            intent.addCategory(Intent.CATEGORY_HOME);
  	            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  	            startActivity(intent);
  	            android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).show();
	}
	
	// check fields of the form
	private boolean checkFields() {
		name = userName.getText().toString();
		if(name.equals("")) {
			showToast("Please fill in user name");
			return false;
		}
		nick = nickname.getText().toString();
		if(nick.equals("")) {
			showToast("Please fill in nick name");
			return false;
		}
		secret = password.getText().toString();
		if(secret.equals("")) {
			showToast("Please fill in password");
			return false;
		}
		if(!secret.equals(confirm.getText().toString())) {
			showToast("Confirm password should be the same with password");
			return false;
		}
		return true;
	}

	private void showToast(String msg) {
		Context context = getApplicationContext();
		int duration = Toast.LENGTH_SHORT;
		Toast.makeText(context, msg, duration).show();
	}
	
	// switch to LogInActivity
	private void toLogIn() {
		Intent intent = new Intent();
		intent.setClass(RegisterActivity.this,
				LogInActivity.class);
		intent.putExtras(bundle);
		startActivity(intent);
		this.finish();
	}
}
